package cs271.examples.recursion;

public class TailRecursiveExponential {

	//here we see various ways to calculate 2^n
	//recursive, iterative, and tail-recursive

	public static int twoToTheN_recursive(int n) {
		if(n==1) return 2;
		return 2 * twoToTheN_recursive(n-1);
	}

	public static int twoToTheN_iterative(int n) {
		int exp = 1;
		for(int i=0;i<n;i++) {
			exp *=2;
		}
		return exp;
	}

	public static int twoToTheN_tailrecursive(int n) {
		return twoToTheN_tailrecursive(n,1);
	}

	public static int twoToTheN_tailrecursive(int n, int current) {
		if(n<1)
			return current;
		else
			return twoToTheN_tailrecursive(n-1, 2*current);
	}

	public static void main(String[] args) {
		System.out.println(twoToTheN_recursive(5));
		System.out.println(twoToTheN_iterative(5));
		System.out.println(twoToTheN_tailrecursive(5));
	}
}